package com.fingard.dsp.bank.directbank.pab02.Util.api.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fingard.dsp.bank.directbank.pab02.Util.api.exception.FtpException;
import com.fingard.dsp.bank.directbank.pab02.Util.api.utils.Des;
import com.fingard.dsp.bank.directbank.pab02.Util.api.utils.FileXmlUtil;
import com.fingard.dsp.bank.directbank.pab02.Util.api.utils.ScrtUtil;

import java.util.HashMap;

/**
 * 文件系统消息类，服务器和客户端都是用该类进行头的交互
 * 
 */
public class FileMsgBean {
	public static Log log = LogFactory.getLog(FileMsgBean.class);

	/* 客户端IP */
	private String clientIp = null;
	/* 文件服务器IP地址  */
	private String serverIp = null;

	/* 文件服务器的文件名称  */
	private String fileName = null;
	/* 客户端本地的文件名称  */
	private String clientFileName = null;

	/* 文件服务器的名称  */
	private String serverName = null;

	/* 用户ID */
	private String uid = null;
	/* 用户 密码 */
	private String passwd = null;
	/* 认证标志  */
	private boolean authFlag = false;

	/* 操作标志  */
	private String fileMsgFlag = null;
	/* 提示信息  */
	private String fileRetMsg = null;

	/* 文件大小  */
	private long fileSize = -1;
	/* 文件分片的序号  */
	private int fileIndex = -1;
	/* 分片的大小 */
	private int pieceNum = -1;

	/* 当前传输内容的大小  */
	private int contLen = 0;
	/* 文件的内容 */
	private byte[] fileCont = null;
	/* 是否是最后一个分片 */
	private boolean lastPiece = false;

	/* MD5的校验码 */
	private String md5 = null;
	/* 加密的标志 */
	private boolean scrtFlag = false;
	/* 加密的密钥 */
	private byte[] desKey = null;

	/* 编码标志 */
	private boolean ebcdicFlag = false;

	/*目标系统名称*/
	private String tarSysName = null;

	/*目标系统文件路径*/
	private String tarFileName = null;

	/*sessionID*/
	private String sessionID = null;

	/*下载文件的私钥授权码*/
	private String privateAuth = null;

	/* token认证标志  */
	private boolean authTokenFlag = false;

	/*门户申请的appId*/
	private String appID = null;

	/*接入方式.渠道码*/
	private String SDKType = null;

	/*token值*/
	private String token = null;

	/*最后一次访问服务器的时间*/
	private long lastUpdateTime = System.currentTimeMillis();

	/*断点续传标识 true 续传*/
	private boolean continueFlag = false;

	/*临时文件大小*/
	private long TmpFileLenth = -1;

	/*续传开始分片*/
	private int startPiece = 0;

	/*远程路径*/
	private String remotePath = null;
	/*本地文件路径*/
	private String localPath = null;

	/*文件服务器URL*/
	private String fileServerUrl = null;

	public String getFileServerUrl() {
		return fileServerUrl;
	}

	public void setFileServerUrl(String fileServerUrl) {
		this.fileServerUrl = fileServerUrl;
	}


	public String getRemotePath() {
		return remotePath;
	}

	public void setRemotePath(String remotePath) {
		this.remotePath = remotePath;
	}

	public String getLocalPath() {
		return localPath;
	}

	public void setLocalPath(String localPath) {
		this.localPath = localPath;
	}




	public String getFileMsgFlag() {
		return fileMsgFlag;
	}

	public void setFileMsgFlag(String fileMsgFlag) {
		this.fileMsgFlag = fileMsgFlag;
	}

	public String getClientIp() {
		return clientIp;
	}

	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getClientFileName() {
		return clientFileName;
	}

	public void setClientFileName(String clientFileName) {
		this.clientFileName = clientFileName;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public boolean isAuthFlag() {
		return authFlag;
	}

	public void setAuthFlag(boolean authFlag) {
		this.authFlag = authFlag;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public int getFileIndex() {
		return fileIndex;
	}

	public void setFileIndex(int fileIndex) {
		this.fileIndex = fileIndex;
	}

	public int getPieceNum() {
		return pieceNum;
	}

	public void setPieceNum(int pieceNum) {
		this.pieceNum = pieceNum;
	}

	public boolean isLastPiece() {
		return lastPiece;
	}

	public void setLastPiece(boolean lastPiece) {
		this.lastPiece = lastPiece;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public boolean isScrtFlag() {
		return scrtFlag;
	}

	public void setScrtFlag(boolean scrtFlag) {
		this.scrtFlag = scrtFlag;
	}

	public byte[] getDesKey() {
		return desKey;
	}

	public void setDesKey(byte[] desKey) {
		this.desKey = desKey;
	}

	public int getContLen() {
		return contLen;
	}

	public String getSessionID() {
		return sessionID;
	}

	public void setSessionID(String sessionID) {
		this.sessionID = sessionID;
	}

	public String getPrivateAuth() {
		return privateAuth;
	}

	public void setPrivateAuth(String privateAuth) {
		this.privateAuth = privateAuth;
	}

	public int getStartPiece() {
		return startPiece;
	}

	public void setStartPiece(int startPiece) {
		this.startPiece = startPiece;
	}

	public void setContLen(int contLen) {
		this.contLen = contLen;
		if (contLen == pieceNum)
			return;
		else {
			for (int start = contLen; start < this.pieceNum; start++) {
				this.fileCont[start] = 0x00;
			}
		}
	}

	public byte[] getFileCont() {
		if (fileCont == null)
			fileCont = new byte[pieceNum];
		return fileCont;
	}

	public void setFileCont(byte[] fileCont1) {
		if (this.fileCont == null) {
			fileCont = new byte[pieceNum];
		}
		System.arraycopy(fileCont1, 0, fileCont, 0, fileCont1.length);
	}

	public int addFileIndex() {
		this.fileIndex++;
		return this.fileIndex;
	}

	public boolean isEbcdicFlag() {
		return ebcdicFlag;
	}

	public void setEbcdicFlag(boolean ebcdicFlag) {
		this.ebcdicFlag = ebcdicFlag;
	}

	public String getFileRetMsg() {
		return fileRetMsg;
	}

	public void setFileRetMsg(String fileRetMsg) {
		this.fileRetMsg = fileRetMsg;
	}

	public String getTarSysName() {
		return tarSysName;
	}

	public void setTarSysName(String tarSysName) {
		this.tarSysName = tarSysName;
	}

	public String getTarFileName() {
		return tarFileName;
	}

	public void setTarFileName(String tarFileName) {
		this.tarFileName = tarFileName;
	}

	public boolean isAuthTokenFlag() {
		return authTokenFlag;
	}

	public void setAuthTokenFlag(boolean authTokenFlag) {
		this.authTokenFlag = authTokenFlag;
	}

	public String getAppID() {
		return appID;
	}

	public void setAppID(String appID) {
		this.appID = appID;
	}

	public String getSDKType() {
		return SDKType;
	}

	public void setSDKType(String sDKType) {
		SDKType = sDKType;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public long getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public boolean isContinueFlag() {
		return continueFlag;
	}

	public void setContinueFlag(boolean continueFlag) {
		this.continueFlag = continueFlag;
	}

	public long getTmpFileLenth() {
		return TmpFileLenth;
	}

	public void setTmpFileLenth(long tmpFileLenth) {
		TmpFileLenth = tmpFileLenth;
	}

	public void convertXmlToHead(String head) throws FtpException {
		if (log.isDebugEnabled())
			log.debug("convertXmlToHead ->>  " + head);
            System.out.println("convertXmlToHead ->>  " + head);
		HashMap<String, String> beanMap = FileXmlUtil.parseXml(head);

		if (beanMap.get("ServerName") != null)
			this.serverName = beanMap.get("ServerName");
		if (beanMap.get("Uid") != null)
			this.uid = beanMap.get("Uid");
		if (beanMap.get("Passwd") != null)
			this.passwd = beanMap.get("Passwd");
		if (beanMap.get("FileMsgFlag") != null)
			this.fileMsgFlag = beanMap.get("FileMsgFlag");
		if (beanMap.get("fileRetMsg") != null)
			this.fileRetMsg = beanMap.get("fileRetMsg");
		if (beanMap.get("AuthFlag") != null)
			this.authFlag = Boolean.parseBoolean(beanMap.get("AuthFlag"));
		if (beanMap.get("ScrtFlag") != null)
			this.scrtFlag = Boolean.parseBoolean(beanMap.get("ScrtFlag"));
		if (beanMap.get("Md5") != null)
			this.md5 = beanMap.get("Md5");
		if (beanMap.get("FileName") != null)
			this.fileName = beanMap.get("FileName");
		if (beanMap.get("ClientFileName") != null)
			this.clientFileName = beanMap.get("ClientFileName");
		if (beanMap.get("LastPiece") != null)
			this.lastPiece = Boolean.parseBoolean(beanMap.get("LastPiece"));
		if (beanMap.get("FileSize") != null)
			this.fileSize = Long.parseLong(beanMap.get("FileSize"));
		if (beanMap.get("FileIndex") != null)
			this.fileIndex = Integer.parseInt(beanMap.get("FileIndex"));
		if (beanMap.get("PieceNum") != null)
			this.pieceNum = Integer.parseInt(beanMap.get("PieceNum"));
		if (beanMap.get("TmpFileLenth") != null)
			this.TmpFileLenth = Long.parseLong(beanMap.get("TmpFileLenth"));
		if (beanMap.get("startPiece") != null)
			this.startPiece = Integer.parseInt(beanMap.get("startPiece"));
		if (beanMap.get("DesKey") != null) {
			byte[] scrtKey = ScrtUtil.decodeBase64(beanMap.get("DesKey"));
			this.desKey = Des.decrypt3DES(scrtKey, Des.getDesKey());
			if (log.isInfoEnabled())
				log.info("密钥同步成功");
		}


		if (beanMap.get("sessionID") != null) {
			this.sessionID = beanMap.get("sessionID");
		}
		if (beanMap.get("privateAuth") != null) {
			this.privateAuth = beanMap.get("privateAuth");
		}
		if (beanMap.get("tarSysName") != null) {
			this.tarSysName = beanMap.get("tarSysName");
		}
		if (beanMap.get("tarFileName") != null) {
			this.tarFileName = beanMap.get("tarFileName");
		}
		if (beanMap.get("authTokenFlag") != null)
			this.authTokenFlag = Boolean.parseBoolean(beanMap.get("authTokenFlag"));

		if (beanMap.get("appID") != null) {
			this.appID = beanMap.get("appID");
		}
		if (beanMap.get("SDKType") != null) {
			this.SDKType = beanMap.get("SDKType");
		}
		if (beanMap.get("token") != null) {
			this.token = beanMap.get("token");
		}
		if (beanMap.get("continueFlag") != null)
			this.continueFlag = Boolean.parseBoolean(beanMap.get("continueFlag"));
	}

	public String convertHeadToXml() throws FtpException {
		HashMap<String, String> beanMap = new HashMap<String, String>();

		if (fileMsgFlag != null) {
			beanMap.put("FileMsgFlag", fileMsgFlag);
		}

		if (fileRetMsg != null) {
			beanMap.put("fileRetMsg", fileRetMsg);
		}

		if (FileMsgType.PUT_AUTH.equals(this.fileMsgFlag)
				|| FileMsgType.GET_AUTH.equals(this.fileMsgFlag)) {
			if (uid != null) {
				beanMap.put("Uid", uid);
			}
			if (serverName != null) {
				beanMap.put("ServerName", serverName);
			}
			if (passwd != null) {
				beanMap.put("Passwd", passwd);
			}
			if (authFlag == true)
				beanMap.put("AuthFlag", Boolean.toString(authFlag));
			if (scrtFlag == true)
				beanMap.put("ScrtFlag", Boolean.toString(scrtFlag));
			if (fileName != null)
				beanMap.put("FileName", fileName);
			if (clientFileName != null)
				beanMap.put("ClientFileName", clientFileName);
			if (fileSize > -1)
				beanMap.put("FileSize", Long.toString(fileSize));

			if(appID != null){
				beanMap.put("appID", appID);
			}
			if(SDKType != null){
				beanMap.put("SDKType", SDKType);
			}
			if(token != null){
				beanMap.put("token", token);
			}
			if (authTokenFlag == true){
				beanMap.put("authTokenFlag", Boolean.toString(authTokenFlag));
			}
			if (continueFlag == true){
				beanMap.put("continueFlag", Boolean.toString(continueFlag));
			}
			if (TmpFileLenth > -1){
				beanMap.put("TmpFileLenth", Long.toString(TmpFileLenth));
			}
			if (startPiece > -1){
				beanMap.put("startPiece", Integer.toString(startPiece));
			}
		}
		if(sessionID != null){
			beanMap.put("sessionID", sessionID);
		}
		if(privateAuth != null){
			beanMap.put("privateAuth", privateAuth);
		}

		if (uid != null)
			beanMap.put("Uid", uid);
		if (fileSize > -1)
			beanMap.put("FileSize", Long.toString(fileSize));
		if (clientFileName != null)
			beanMap.put("ClientFileName", clientFileName);
		if (fileName != null)
			beanMap.put("FileName", fileName);
		if (md5 != null)
			beanMap.put("Md5", md5);
		if (fileIndex > -1)
			beanMap.put("FileIndex", Integer.toString(fileIndex));
		if (pieceNum > -1)
			beanMap.put("PieceNum", Integer.toString(pieceNum));
		if (lastPiece == true)
			beanMap.put("LastPiece", Boolean.toString(lastPiece));
		if (tarSysName != null)
			beanMap.put("tarSysName", tarSysName);
		if (tarFileName != null)
			beanMap.put("tarFileName", tarFileName);
		String xml = FileXmlUtil.createXML(beanMap);
		if (log.isDebugEnabled())
			log.debug("convertHeadToXml ->>  " + xml);
		    System.out.println("convertHeadToXml ->>  " + xml);
		return xml;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("FileName=").append(fileName).append(";");
		sb.append("Uid=").append(uid).append(";");
		sb.append("Passwd=").append(passwd).append(";");
		sb.append("MsgType=").append(fileMsgFlag).append(";");
		sb.append("AuthFlag=").append(authFlag).append(";");
		sb.append("LashPiece=").append(lastPiece).append(";");
		sb.append("AuthFlag=").append(authFlag).append(";");
		sb.append("PieceNum=").append(pieceNum).append(";");
		sb.append("DesKey=").append(desKey).append(";");
		sb.append("tarSysName=").append(tarSysName).append(";");
		sb.append("tarFileName=").append(tarFileName).append(";");
		return sb.toString();
	}

	
}
